package com.itheima.admin.controller;

import com.itheima.admin.dto.ChannelPageRequestDto;
import com.itheima.admin.pojo.AdChannel;
import com.itheima.admin.service.AdChannelService;
import com.itheima.common.vo.PageResultVo;
import com.itheima.common.vo.ResultVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * @version 1.0
 * @description 说明
 * @package com.itheima.admin.controller
 */
@RestController
@RequestMapping("/channel")
@Api(tags = "频道管理")
public class AdchannelController {
    @Autowired
    private AdChannelService adChannelService;
    /**
     * 频道列表
     * @param dto
     * @return
     */
    @PostMapping("/list")
    @ApiOperation(value = "频道列表分页查询value",notes="频道列表分页查询Notes")
    public PageResultVo pageResult(@RequestBody ChannelPageRequestDto dto){
        PageResultVo vo = adChannelService.findPage(dto);
        return vo;
    }

    /**
     * 新增频道
     * @param adChannel
     * @return
     */
    @PostMapping("/save")
    @ApiOperation("新增频道")
    // @Validated 要校验参数的合法性, 使用什么规则来校验， 找实体类上的注解
    public ResultVo add(@RequestBody @Validated AdChannel adChannel){
        adChannelService.add(adChannel);
        return ResultVo.ok();
    }

    /**
     * 通过id查询
     * @param channelId
     * @return
     */
    @GetMapping("/{channelId}")
    @ApiOperation("通过id查询")
    public ResultVo findById(@PathVariable(value = "channelId") Integer channelId){
        AdChannel adChannel = adChannelService.getById(channelId);
        return ResultVo.ok(adChannel);
    }

    /**
     * 通过id更新
     * @param adChannel
     * @return
     */
    @PostMapping("/update")
    @ApiOperation("更新")
    public ResultVo update(@RequestBody AdChannel adChannel){
        adChannelService.updateById(adChannel);
        return ResultVo.ok();
    }

    /**
     * 通过id删除
     * @param adChannelId
     * @return
     */
    @ApiOperation("通过id删除")
    @DeleteMapping("/{adChannelId}")
    public ResultVo deleteById(@PathVariable(value = "adChannelId") Integer adChannelId){
        adChannelService.removeById(adChannelId);
        return ResultVo.ok();
    }
}
